<template>
  <div id="fd-request">
    <div class="card">
      <div class="card-body">
        <form class="row row-cols-lg-auto align-items-center">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="航空公司"
              v-model.trim="carrier"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="出发机场"
              v-model.trim="dport"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="到达机场"
              v-model.trim="aport"
            />
          </div>
          <div class="col-1">
            <label class="form-check-label">
              <input
                type="radio"
                class="form-check-input"
                name="sc.status"
                value="0"
                v-model.number="rqStatus"
              />等待中
            </label>
            <label class="form-check-label">
              <input
                type="radio"
                class="form-check-input"
                name="sc.status"
                value="1"
                v-model.number="rqStatus"
              />已完成
            </label>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click.stop="search()"
            >
              查找
            </button>

            <span class="ms-auto">
              <button
                type="button"
                @click.stop="deleteAllFdRequest()"
                class="btn btn-sm btn-warning"
              >
                删除所有
              </button>
              <button
                type="button"
                @click.stop="newFdRequest()"
                class="btn btn-success ms-4"
              >
                新建
              </button>
              <button
                type="button"
                class="btn btn-success ms-2"
                @click.stop="importExcel()"
              >
                导入Excel
              </button>
            </span>
          </div>
        </form>
      </div>
    </div>
    <table class="table table-response table-striped table-sm" id="tableData">
      <thead>
        <tr>
          <th>航空公司</th>
          <th>出发城市</th>
          <th>到达城市</th>
          <th>FD日期</th>
          <th>状态</th>
          <th>创建时间</th>
          <th>下载时间</th>
          <th>备注</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(fd, index) in dataList" :key="index">
          <td>{{ fd.carrier }}</td>
          <td>{{ fd.dport }}</td>
          <td>{{ fd.aport }}</td>
          <td>{{ fd.ddate }}</td>
          <td>{{ getStatusDesc(fd.status) }}</td>
          <td>{{ fd.createTime }}</td>
          <td>{{ fd.checkTime }}</td>
          <td>{{ fd.remark }}</td>
          <td><a href="#" @click.stop="deleteFdRequest(fd.id)">删除</a></td>
        </tr>
      </tbody>
    </table>

    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-excel-upload ref="modalUploadExcel">
      用Excel导入下载请求
      <template v-slot:excelFormatDesc
        >Excel格式说明: 航司二字代码，出发机场，到达机场，日期(可空)</template
      >
    </my-modal-excel-upload>
    <my-modal-fd-request ref="fdRequestModal"></my-modal-fd-request>
  </div>
</template>

<script>
import $ from "jquery";
import {
  searchFdRequests,
  deleteAllFdRequest,
  deleteFdRequest,
  createFdRequest,
  uploadFdRequest,
} from "@/api/misc.js";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyPagination from "@/components/my-pagination.vue";
import MyModalExcelUpload from "@/components/modal/my-modal-excel-upload.vue";
import MyModalFdRequest from "@/components/modal/my-modal-fd-request.vue";

export default {
  components: {
    MyDatePicker,
    MyPagination,
    MyModalExcelUpload,
    MyModalFdRequest
  },
  data() {
    return {
      dataList: [],
      id: 0,

      carrier: "",
      dport: "",
      aport: "",
      rqStatus: 0,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 25,
        pageTotal: 0,
      },

      
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.carrier": this.carrier,
        "sc.dport": this.dport,
        "sc.aport": this.aport,
        "sc.status": this.rqStatus,
      };
      searchFdRequests(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    quickSearch: function (status) {
      this.status = status;
      this.search();
    },
    getStatusDesc: function (status) {
      if (status === 0) return "等待中";
      else if (status === 1) return "已完成";
    },
    deleteFdRequest: function (id) {
      const params = { id };
      deleteFdRequest(params, (v) => {
        this.showErrMsg(v.errmsg);
        if (v.status === "OK") {
          this.search();
        }
      });
    },
    deleteAllFdRequest: function () {
      deleteAllFdRequest((v) => {
        if (v.status === "OK") {
          this.showErrMsg(v.errmsg);
          this.search();
        } else {
          this.showErrMsg(v.errmsg);
        }
      });
    },
    newFdRequest: function () {
      this.$refs.fdRequestModal.modal().then(v => this.save(v));
    },
    save: function (params) {
      this.showErrMsg("后台保存中，请耐心等候");

      createFdRequest(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          this.showErrMsg("保存完毕");
        } else {
          this.showErrMsg("保存失败：" + jsonResult.errmsg);
        }
      });
    },
    importExcel: function () {
      this.$refs.modalUploadExcel
        .modal()
        .then((formData) => {
          // console.log(formData.data)
          this.showLoading("处理中......");
          uploadFdRequest(
            formData.data,
            (v) => {
              if (v.status === "OK") {
                this.showErrMsg("操作成功");
              } else {
                this.showErrMsg("操作失败:" + v.errmsg, "danger");
              }
            },
            () => {
              this.hideLoading();
            }
          );
        })
        .catch((ex) => {});
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>